package com.baidu.bos.web.action.user;

import java.io.IOException;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.baidu.bos.domain.User;
import com.baidu.bos.service.IUserService;
import com.baidu.bos.utils.BOSContext;
import com.baidu.bos.utils.MD5Utils;
import com.baidu.bos.web.action.base.BaseAction;
@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User>{
	private String checkcode;
	private String[] roleIds;
	public void setRoleIds(String[] roleIds) {
		this.roleIds = roleIds;
	}
	public String userIds;


	public void setUserIds(String userIds) {
		this.userIds = userIds;
	}
	@Resource
	private IUserService userService;
	/**
	 * 用户登录
	 */
	public String login(){
		//首先进行验证码的校验
		//从session中获得正确的验证码
		String validatecode = (String) ServletActionContext.getRequest().getSession().getAttribute("key");
		//确定验证码是否正确
		if (StringUtils.isBlank(checkcode) || !validatecode.equals(checkcode) ) {
			//验证码输入错误
			this.addActionError(this.getText("checkCodeError"));
			return "login";
		}else {
			//使用shiro提供的方式进行权限认证
			Subject subject = SecurityUtils.getSubject();
			//从model中获取用户名和密码,然后交给安全管理
			String username = model.getUsername();
			String password = model.getPassword();
			//对密码进行Md5加密
			password = MD5Utils.md5(password);
			AuthenticationToken token = new UsernamePasswordToken(username, password);
			try {
				subject.login(token);
				User user = (User) subject.getPrincipal();
				ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
			} catch (UnknownAccountException unknownAccountException) {
				unknownAccountException.printStackTrace();
				this.addActionError("用户名不存在!");
				return "login";
			} catch (IncorrectCredentialsException incorrectCredentialsException) {
				incorrectCredentialsException.printStackTrace();
				this.addActionError("密码错误!");
				return "login";
			}
			return "home";
			
			/*//验证码输入正确
			User user = userService.login(model);
			if (user != null) {
				//登录成功
				ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
				return "home";
			}else {
				//用户名或密码输入错误
				this.addActionError(this.getText("loginFail"));
				return "login";
			}*/
		}
	}
	/**
	 * 用户注销
	 * @param checkcode
	 */
	public String logout(){
		ServletActionContext.getRequest().getSession().invalidate();
		return "login";
	}
	/**
	 * 修改密码
	 * @param checkcode
	 * @throws IOException 
	 */
	public String editPassword() throws IOException{
		//获取新密码
		String password = model.getPassword();
		//获得用户的id
		String id = BOSContext.getLoginUser().getId();
		String date = "1";
		try {
			userService.editPassword(password,id);
		} catch (Exception e) {
			e.printStackTrace();
			date = "0";
		}
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(date);
		return NONE;
	}
	//保存用户的操作
	public String save(){
		userService.save(model,roleIds);
		return "list";
	}
	
	//分页查询所有用户
	public String pageQuery(){
		  userService.pageQuery(pageBean);
		  this.writeObject2Json(pageBean);
		return NONE;
	}
	
	//删除用户
	public String delete(){
		String flag = "1";
		try {
			userService.delete(userIds);
		} catch (Exception e) {
			e.printStackTrace();
			flag = "0";
		}
		this.writeObject2Json(flag);
		return NONE;
	}
	
	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}
}
